#!/usr/bin/env python3
"""
Qwen3-4B-AWQ模型使用示例

此脚本展示了如何使用QwenModelClient与已部署的Qwen3-4B-AWQ模型进行交互。
"""

import sys
import os
import time

# 添加当前目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from qwen_model_client import QwenModelClient, ModelConfig


def main():
    """主函数"""
    print("="*60)
    print("Qwen3-4B-AWQ模型使用示例")
    print("="*60)
    
    # 初始化客户端
    config = ModelConfig(
        host="192.168.1.236",
        port=8000,
        timeout=2*60
    )
    client = QwenModelClient(config)
    
    # 健康检查
    print("正在进行健康检查...")
    if not client.health_check():
        print("❌ 健康检查失败，请确保模型服务正在运行")
        return
    
    print("✅ 健康检查通过")
    
    # 获取可用模型列表
    try:
        models = client.get_available_models()
        print(f"可用模型: {models}")
    except Exception as e:
        print(f"获取模型列表失败: {str(e)}")
    
    # 示例1: 简单对话
    print("\n示例1: 简单对话")
    print("-" * 40)
    prompt = "你好，请用一句话介绍你自己。"
    print(f"用户: {prompt}")
    
    try:
        start_time = time.time()
        response = client.simple_chat(prompt)
        end_time = time.time()
        
        print(f"模型: {response}")
        print(f"响应时间: {end_time - start_time:.2f}秒")
    except Exception as e:
        print(f"错误: {str(e)}")
    
    # 示例2: 多轮对话
    print("\n示例2: 多轮对话")
    print("-" * 40)
    conversation = [
        {"role": "user", "content": "请解释什么是机器学习"},
        {"role": "assistant", "content": "机器学习是人工智能的一个分支，它使计算机系统能够从数据中学习和改进，而无需明确编程。"},
        {"role": "user", "content": "机器学习有哪些主要类型？"}
    ]
    
    for i, message in enumerate(conversation):
        role = message["role"]
        content = message["content"]
        print(f"{'用户' if role == 'user' else '模型'}: {content}")
        
        if role == "user" and i == len(conversation) - 1:  # 最后一条用户消息
            try:
                start_time = time.time()
                response = client.chat_completion(conversation)
                end_time = time.time()
                
                assistant_message = response["choices"][0]["message"]["content"]
                print(f"模型: {assistant_message}")
                print(f"响应时间: {end_time - start_time:.2f}秒")
            except Exception as e:
                print(f"错误: {str(e)}")
    
    # 示例3: 参数控制
    print("\n示例3: 参数控制")
    print("-" * 40)
    prompt = "请写一首关于春天的诗"
    
    # 低温度，更确定的输出
    print(f"用户: {prompt} (温度=0.1)")
    try:
        response = client.simple_chat(prompt, temperature=0.1)
        print(f"模型(低温度): {response[:100]}...")
    except Exception as e:
        print(f"错误: {str(e)}")
    
    # 高温度，更随机的输出
    print(f"\n用户: {prompt} (温度=1.0)")
    try:
        response = client.simple_chat(prompt, temperature=1.0)
        print(f"模型(高温度): {response[:100]}...")
    except Exception as e:
        print(f"错误: {str(e)}")
    
    print("\n示例运行完成！")


if __name__ == "__main__":
    main()